package com.sig.agent;

import java.util.ArrayList;
import java.util.List;

/**
 * 热更文件历史记录
 *
 * @author sig
 * @since 2019/3/6
 */
public class ClassFileUpdate {
    private String className;
    private List<FileUpdateHistory> updateHistories = new ArrayList<>();

    public ClassFileUpdate(String className) {
        this.className = className;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public void add2UpdateHistory(long fileLastModifyTime) {
        updateHistories.add(0, new FileUpdateHistory(fileLastModifyTime));
    }

    public long lastModifyTime() {
        if (!updateHistories.isEmpty()) {
            FileUpdateHistory fileUpdateHistory = updateHistories.get(0);
            if (fileUpdateHistory != null)
                return fileUpdateHistory.getFileLastModifyTime();
        }
        return 0L;
    }

    public long lastReloadTime() {
        if (!updateHistories.isEmpty()) {
            FileUpdateHistory fileUpdateHistory = updateHistories.get(0);
            if (fileUpdateHistory != null)
                return fileUpdateHistory.getReloadTime();
        }
        return 0L;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(90);
        builder.append("ClassFileUpdate [")
                .append("className=").append(className)
                .append(", updateHistories=").append(updateHistories)
                .append("]");
        return builder.toString();
    }

    class FileUpdateHistory {
        private long fileLastModifyTime;
        private long reloadTime;

        public FileUpdateHistory(long fileLastModifyTime) {
            this.fileLastModifyTime = fileLastModifyTime;
            this.reloadTime = System.currentTimeMillis();
        }

        public long getFileLastModifyTime() {
            return fileLastModifyTime;
        }

        public void setFileLastModifyTime(long fileLastModifyTime) {
            this.fileLastModifyTime = fileLastModifyTime;
        }

        public long getReloadTime() {
            return reloadTime;
        }

        public void setReloadTime(long reloadTime) {
            this.reloadTime = reloadTime;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder(90);
            builder.append("FileUpdateHistory [")
                    .append("fileLastModifyTime=").append(fileLastModifyTime)
                    .append(", reloadTime=").append(reloadTime)
                    .append("]");
            return builder.toString();
        }
    }
}
